// pages/equity/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "我的权益", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      isShare: 0
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    tabId: 0,

    openid: '',
    shareTitle: '',
    shareImg: '',
    showList: [],
    list: [],
    historyList: [],
    dayList: [],
    chooseDayId: 0,
    scrollLeft: 0,
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  recommend() {
    let that = this;
    that.setData({
      tabId: 0,
      showList: that.data.list
    })
  },
  historyRecommend() {
    let that = this;
    that.setData({
      tabId: 1,
      showList: that.data.historyList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');
    //openid = 'oZYOb4ktAvmrb5TsIPBHlrGEKcZg';
    var is_share = options.is_share;
    //判断返回的跳转路径
    if (is_share == 1) {
      that.setData({
        ['defaultData.isShare']: 1
      });
    }

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getEquityPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getEquityPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },


  //获取本业数据
  getEquityPageInfo(openid) {
    var that = this;
    //获取发放权益的缓存
    var is_send_welfare = 0;
    var sendTimestamp = wx.getStorageSync('sendTimestamp');
    if (sendTimestamp == '' || sendTimestamp == null || sendTimestamp == undefined) {
      var timestamp = new Date().getTime();
      //缓存权益发放标识
      wx.setStorageSync('sendTimestamp', timestamp);
      is_send_welfare = 1;
    } else {
      //存在发放标识
      var timestamp = new Date().getTime();
      if (timestamp - sendTimestamp > 10000) {
        //缓存已超过10s
        is_send_welfare = 1;
        //重置发放时间戳
        wx.setStorageSync('sendTimestamp', timestamp);
      }
    }
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getEquityPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'is_send_welfare': is_send_welfare
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            list: res.data.list,
            historyList: res.data.historyList,
            showList: res.data.list,
            dayList: res.data.dayList,
            chooseDayId: res.data.dayList.length - 1,
            scrollLeft: (res.data.dayList.length - 1) * 66,
          });

          console.log(that.data.showList);
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //切换月份
  chooseDay(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      chooseDayId: id
    });
    //获取当前的年月
    var ym = that.data.dayList[id].ym;
    //后台切换权益
    wx.showLoading({
      title: '权益切换中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getEquityListToMonth.php',
      method: 'POST',
      data: {
        openid: that.data.openid,
        ym: ym
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            showList: res.data.historyList
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
      }
    });
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {
        id: 31,
        pid: 3
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //跳往权益详情页
  goDetail(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var mid = e.currentTarget.dataset.mid;
    var syType = e.currentTarget.dataset.type; //0已过期 1可使用 2:已使用完
    wx.navigateTo({
      url: '/pages/equity/detail?id=' + id + "&mid=" + mid + "&syType=" + syType
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/equity/index?is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})